# 校验 登录信息 注册信息
from datetime import datetime
from func import read_data, save_flow, save_data

login_user_dict = {'username': '',
                   "bank_id": '',
                   "bank_pay_pwd": "",
                   "balance": 0
                   }  # 登录情况收集


def init_login_dict(user):
    data_dict = read_data('info', 'all_info.txt')
    login_dict = data_dict[user]
    login_user_dict['username'] = user
    login_user_dict['bank_id'] = login_dict['id']
    login_user_dict['bank_pay_pwd'] = login_dict['pay_pwd']
    login_user_dict['balance'] = int(login_dict['balance'])


# 登录验证
def check_login(**kwargs):
    username, password = kwargs.get('username'), kwargs.get('password')
    data_dict = read_data(tag='pwd', path='all_pwd.txt')
    # {'dream': {'pwd': '521'}, 'user': {'pwd': '001'}}
    if username not in data_dict:
        return False, f"用户名尚未注册!"
    if password != data_dict[username]['pwd']:
        return False, f"密码错误！"
    else:
        txt = f"{username}登录成功"
        save_flow('save', user=username,
                  txt=f"{txt}>>>时间为>>>{datetime.now().strftime('%Y年%m月%d日 %H时%M分%S秒')}")
        return True, txt


def check_info(**kwargs):
    username = kwargs.get('username')
    bank_id_pwd = ''
    balance = ''
    while True:
        print("信息录入".center(10, '>'))
        bank_id = input("请输入银行卡号(4位数字)：").strip()
        if not bank_id.isdigit():
            print("银行卡号不可以出现非法字符！")
            continue
        elif len(bank_id) != 4:
            print("银行卡号必须4个字符！")
            continue
        bank_id_pwd = input("请输入取款密码(4位密码)：").strip()
        if len(bank_id_pwd) > 4:
            print("请不要输入超过4个字符！")
            continue
        balance = 1000  # 默认注册时为1000
        break
    save_data('info', username=username, bank_id=bank_id, pay_pwd=bank_id_pwd, balance=balance)
